﻿using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System;

namespace HiStaff.Control
{
    [ToolboxBitmap(typeof(NumericUpDown))]
    public class HiNumericTextBox : Janus.Windows.GridEX.EditControls.NumericEditBox
    {
        bool _isRequire = false;
        bool _returnValueZeroWhenNull = false;
        private bool _readOnlyWhenEdit = false;
        private bool _searchControl = false;

        public bool SearchControl
        {
            get { return _searchControl; }
            set { _searchControl = value; }
        }
        public bool ReadOnlyWhenEdit
        {
            get { return _readOnlyWhenEdit; }
            set { _readOnlyWhenEdit = value; }
        }
        [DefaultValue(false)]
        public bool IsRequire
        {
            get
            {
                return _isRequire;
            }
            set
            {
                _isRequire = value;
                if (value)
                    this.BackColor = HiStaff.iSystem.Data.COLOR_REQUIREMENT;
                else this.BackColor = HiStaff.iSystem.Data.COLOR_NONREQUIREMENT;
            }
        }
        public string DataField { get; set; }
        [DefaultValue(false)]
        public bool ReturnValueZeroWhenNull { get { return _returnValueZeroWhenNull; } set { _returnValueZeroWhenNull = value; } }
        public decimal? Value
        {
            get
            {
                if (_returnValueZeroWhenNull)
                {
                    try
                    {
                        if (base.Value == null) return 0;
                        return Convert.ToDecimal(base.Value);
                    }
                    catch
                    {
                        return 0;
                    }
                }
                else
                {
                    try
                    {
                        if (base.Value == null) return null;
                        return Convert.ToDecimal(base.Value);
                    }
                    catch
                    {
                        return null;
                    }
                }
            }
            set
            {
                base.Value = value;
            }
        }
        
        public HiNumericTextBox()
        {
            this.VisualStyle = Janus.Windows.GridEX.VisualStyle.Office2010;
            this.NullBehavior = Janus.Windows.GridEX.NumericEditNullBehavior.AllowNull;
            this.FormatString = "#,##0.##";
        }
    }
}
